//
//  UIButton+Category.m
//  YLB
//
//  Created by Zoo Chow on 17/02/2017.
//  Copyright © 2017 Zoo. All rights reserved.
//

#import "UIButton+Category.h"
#import "UIImage+Category.h"

@implementation UIButton (Category)


+ (UIButton*)createTitleButton:(NSString *)title
                   normalColor:(UIColor *)normalColor
                highlightColor:(UIColor *)highlightColor
                         frame:(CGRect)frame
                          font:(UIFont *)font
                           tag:(NSInteger)tag
                        target:(id)target
                        action:(SEL)action {
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:frame];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:normalColor forState:UIControlStateNormal];
    [button setTitleColor:highlightColor forState:UIControlStateHighlighted];
    [button setTag:tag];
    [button.titleLabel setFont:font];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

+ (UIButton*)createImageButton:(CGRect)frame
                   normalImage:(UIImage *)normalImage
                highlightImage:(UIImage *)highlightImage
                           tag:(NSInteger)tag
                        target:(id)target
                        action:(SEL)action {
    UIButton * button = [UIButton createTitleButton:nil normalColor:nil highlightColor:nil frame:frame font:nil tag:tag target:target action:action];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:highlightImage forState:UIControlStateHighlighted];
    return button;
}

+ (UIButton*)createImageButton:(CGRect)frame
               normalImageName:(NSString *)normalImageName
            highlightImageName:(NSString *)highlightImageName
                           tag:(NSInteger)tag
                        target:(id)target
                        action:(SEL)action  {
    UIButton * button = [UIButton createTitleButton:nil normalColor:nil highlightColor:nil frame:frame font:nil tag:tag target:target action:action];
    [button setImage:[UIImage imageNamed:normalImageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:highlightImageName] forState:UIControlStateHighlighted];
    return button;
}

+ (UIButton*)createTitleButton:(NSString *)title
                   normalColor:(UIColor *)normalColor
                highlightColor:(UIColor *)highlightColor
                   normalImage:(UIImage *)normalImage
                highlightImage:(UIImage *)highlightImage
                         frame:(CGRect)frame
                          font:(UIFont *)font
                           tag:(NSInteger)tag
                        target:(id)target
                        action:(SEL)action {
    UIButton * button = [UIButton createTitleButton:title
                                        normalColor:normalColor
                                     highlightColor:highlightColor
                                              frame:frame
                                               font:font
                                                tag:tag
                                             target:target
                                             action:action];
    [button setBackgroundImage:normalImage forState:UIControlStateNormal];
    [button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
    return button;
}

+ (UIButton *)createTitleButton:(NSString *)title
                    normalColor:(UIColor *)normalColor
                 highlightColor:(UIColor *)highlightColor
               normalImageColor:(UIColor *)normalImageColor
            highlightImageColor:(UIColor *)highlightImageColor
                          frame:(CGRect)frame
                           font:(UIFont *)font
                            tag:(NSInteger)tag
                         target:(id)target
                         action:(SEL)action {
    UIButton * button = [UIButton createTitleButton:title
                                        normalColor:normalColor
                                     highlightColor:highlightColor
                                              frame:frame
                                               font:font
                                                tag:tag
                                             target:target
                                             action:action];
    [button setBackgroundImage:[UIImage imageOfPixelWithColor:normalImageColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageOfPixelWithColor:highlightImageColor] forState:UIControlStateHighlighted];
    //[button.layer setBorderColor:normalImageColor.CGColor];
    //[button.layer setBorderWidth:1.0f];
    return button;
    
}

- (void)setTitleInSource:(NSArray *)source forState:(UIControlState)state {
    NSNumber * fontString = [self.titleLabel.font.fontDescriptor objectForKey:@"NSFontSizeAttribute"];
    CGFloat height = fontString.floatValue+4;
    NSMutableAttributedString * attributed = [[NSMutableAttributedString alloc]initWithString:@""];
    for (NSInteger i = 0; i < source.count; i++) {
        id obj = [source objectAtIndex:i];
        if ([obj isKindOfClass:[UIImage class]]) {
            NSTextAttachment * attachment = [[NSTextAttachment alloc]init];
            attachment.image = (UIImage*)obj;
            attachment.bounds = CGRectMake(0, -4, height, height);
            NSAttributedString * ats = [NSAttributedString attributedStringWithAttachment:attachment];
            [attributed appendAttributedString:ats];
        } else if ([obj isKindOfClass:[NSString class]]) {
            NSAttributedString * ats = [[NSAttributedString alloc] initWithString:(NSString *)obj];
            [attributed appendAttributedString:ats];
        }
    }
    [self setAttributedTitle:attributed forState:state];
}

@end





































